# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libmariadbclient
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.1.11
pkgrel=1
pkgdesc="MariaDB client libraries (mingw-w64)"
arch=('any')
url="https://mariadb.org/"
license=('LGPL')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-tools")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-curl"
         #"${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-zlib")
#optdepends=("${MINGW_PACKAGE_PREFIX}-curl")
options=('!strip' 'staticlibs')
source=(#"https://downloads.mariadb.org/interstitial/connector-c-${pkgver}/source-tgz/mariadb-connector-c-${pkgver}-src.tar.gz"
        http://mirror.mephi.ru/mariadb/connector-c-${pkgver}/mariadb-connector-c-${pkgver}-src.tar.gz
        001-mingw-build.patch
        002-fix-prototype.patch
        003-gcc-fix-use_VA_ARGS.patch)
validpgpkeys=("199369E5404BD5FC7D2FE43BCBCB082A1BB943DB") #MariaDB Package Signing Key <package-signing-key@mariadb.org>
sha256sums=('3e6f6c399493fe90efdc21a3fe70c30434b7480e8195642a959f1dd7a0fa5b0f'
            '1ab3365f656be15a04c1bc30434346696f749062ec9dbffdc69f06241e649c32'
            '7f2231363c2b21bfd578c4600ca0fad33fb8ebf2a463bc3b42f202c9ff7ef366'
            '18d3bd0b06906e7ac295b024f1d94d9852ede0a28f06da0d51b452c2d896b432')

prepare() {
  cd ${srcdir}/mariadb-connector-c-${pkgver}-src
  patch -p1 -i ${srcdir}/001-mingw-build.patch
  patch -p1 -i ${srcdir}/002-fix-prototype.patch
  patch -p1 -i ${srcdir}/003-gcc-fix-use_VA_ARGS.patch
}

build() {
  rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST}
  cd ${srcdir}/build-${MINGW_CHOST}
  
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G "MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=RELEASE \
    -DWITH_EXTERNAL_ZLIB=ON \
    -DWITH_MYSQLCOMPAT=OFF \
    -DWITH_SSL="SCHANNEL" \
    -DCLIENT_PLUGIN_AUTH_GSSAPI_CLIENT=STATIC \
    -DCLIENT_PLUGIN_DIALOG=STATIC \
    -DCLIENT_PLUGIN_REMOTE_IO=OFF \
    -DCLIENT_PLUGIN_PVIO_NPIPE=STATIC \
    -DCLIENT_PLUGIN_PVIO_SHMEM=STATIC \
    -DCLIENT_PLUGIN_CLIENT_ED25519=OFF \
    -DCLIENT_PLUGIN_CACHING_SHA2_PASSWORD=STATIC \
    -DCLIENT_PLUGIN_SHA256_PASSWORD=STATIC \
    -DCLIENT_PLUGIN_MYSQL_CLEAR_PASSWORD=STATIC \
    -DCLIENT_PLUGIN_MYSQL_OLD_PASSWORD=STATIC \
    ../mariadb-connector-c-${pkgver}-src

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install

  ln -s "${pkgdir}${MINGW_PREFIX}"/include/mariadb "${pkgdir}${MINGW_PREFIX}"/include/mysql

  if [ "${CARCH}" = "i686" ]; then
    rm -f ${pkgdir}${MINGW_PREFIX}/lib/libmariadb.dll.a
    ${MINGW_PREFIX}/bin/gendef -a ${pkgdir}${MINGW_PREFIX}/bin/libmariadb.dll
    ${MINGW_PREFIX}/bin/dlltool -k -d libmariadb.def -l ${pkgdir}${MINGW_PREFIX}/lib/libmariadb.dll.a
  fi

  cp "${pkgdir}${MINGW_PREFIX}"/lib/{libmariadb,libmysqlclient}.dll.a
  cp "${pkgdir}${MINGW_PREFIX}"/lib/{libmariadb,libmysqlclient_r}.dll.a
  cp "${pkgdir}${MINGW_PREFIX}"/lib/{libmariadbclient,libmysqlclient}.a
  cp "${pkgdir}${MINGW_PREFIX}"/lib/{libmariadbclient,libmysqlclient_r}.a

  local _PRE_WIN="$(cygpath -m ${MINGW_PREFIX})"

  for pcfile in  "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${_PRE_WIN}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done
 }
